var quips = {
    onLoad: function(){
        gBrowser.addProgressListener(myListener);
        SetUserState();
        $('#browser1').resize(function(){
            var currentset = document.getElementById("nav-bar").currentSet;
            currentset = currentset + ",userState";
            document.getElementById("nav-bar").setAttribute("currentset", currentset);
            document.getElementById("nav-bar").currentSet = currentset;
            document.persist("nav-bar", "currentset");
            $('#browser1').contents().find('#numelem').html(Number($('#browser1').attr('height') - 45) / 92);
        });
        var browser = document.getElementById("browser1"); // browser
        if (browser) 
            browser.addEventListener("DOMContentLoaded", quips.onPageLoad, true);
        this.initialized = true;
    },
    onPageLoad: function(aEvent){
    
        var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
        LoadURL();
        aEvent.originalTarget.defaultView.addEventListener("unload", function(){
            quips.onPageUnload();
        }, true);
        $('#browser1').contents().find('#numelem').html(Number($('#browser1').attr('height') - 45) / 112);
    },
    onPageUnload: function(aEvent){
        // do something
    }
};

window.addEventListener("load", function(e){


    quips.onLoad(e);
}, false);

const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
var myListener = {
    QueryInterface: function(aIID){
        if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
        aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
        aIID.equals(Components.interfaces.nsISupports)) 
            return this;
        throw Components.results.NS_NOINTERFACE;
    },
    
    onStateChange: function(aWebProgress, aRequest, aFlag, aStatus){
    
        if (aFlag & STATE_START) {
        
        }
        if (aFlag & STATE_STOP) {
        
        }
    },
    
    onLocationChange: function(aProgress, aRequest, aURI){
        LoadURL();
        SetCountComments();
    }
    
}
function LoadURL(){
    if (gBrowser.contentDocument.location == 'about:home' || gBrowser.contentDocument.location == 'about:config') {
        $('#browser1').attr('src', 'http://quips.heydorn.biz/welcome.php');
    }
    else {
        $('#browser1').attr('src', 'http://quips.heydorn.biz/comments.php?url=' + sha1(gBrowser.contentDocument.location));
    }
}

function ToggleDisplay(){
    $('#browser1').toggle();
    $('#splitter').toggle();
}



function SetCountComments(){

    $.ajax({
        url: 'http://quips.heydorn.biz/countComments.php?url=' + sha1(gBrowser.contentDocument.location),
        success: function(data){
            $('#userState').attr('tooltiptext', data + ' comments');
        }
    });
}



var loggedIn = false;
function SetUserState(){

    $.ajax({
        url: 'http://quips.heydorn.biz/checkUserState.php',
        dataType: 'json',
        success: function(data){
            $.each(data, function(key, val){
                if (key = "loggedIn") {
                    loggedIn = val;
                    if (loggedIn) {
                        $('#registerButton').hide();
                        $('#loginButton').hide();
                        $('#logoutButton').show();
                    }
                    else {
                        $('#registerButton').show();
                        $('#loginButton').show();
                        $('#logoutButton').hide();
                    }
                    SetUserStateImage(val);
                }
            });
        }
    });
}

function SetUserStateImage(loggedIn){
    var path = 'http://quips.heydorn.biz/image/';
    path += loggedIn ? 'IconConnected.png' : 'IconDisconnected.png';
    
    $('#userState').attr('image', path);
    document.getElementById('browser1').contentDocument.location.reload(true);
    
}

function handleBottonClick(el){
    v = el.getAttribute("value");
    switch (v) {
        case ('Toggle'):
            ToggleDisplay();
            break;
        default:
            ShowDialog(v);
            break;
    }
    
}

function ShowDialog(dialogXUL){
    var dialog;
    dialog = window.openDialog("chrome://quips/content/" + dialogXUL, "showmore", "chrome", "aa");
    dialog.addEventListener("unload", function(e){
    
        SetUserState();
    }, false);
}

